package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

public class UpdateUserDemo {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        System.out.println("修改用户信息");
        System.out.print("请输入需要修改的用户名:");
        String name = scan.next();
        System.out.print("请输入新的密码:");
        String new_pwd = scan.next();
        System.out.print("请输入新的昵称:");
        String new_nickname = scan.next();

        Connection connection = DBUtil.getConnection();
        System.out.println("与数据库链接成功");

        Statement statement = connection.createStatement();

        String sql = "UPDATE userinfo " +
                "SET password = '" + new_pwd + "',nickname = '" + new_nickname + "' " +
                "WHERE username = '" + name + "'";

        int num = statement.executeUpdate(sql);
        System.out.println(num > 0 ? "修改成功" : "用户名不存在");
        statement.close();
    }
}
